package com.santiao.youxiang;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;

import com.santiao.publics.base.BaseApplication;
import com.santiao.publics.config.Const;
import com.santiao.publics.util.LogUtil;
import com.santiao.publics.util.MyStringUtil;
import com.santiao.publics.util.SharedPreferencesUtil;
import com.santiao.publics.util.UmengUtil;
//import com.santiao.publics.util.oaid.DemoHelper;
import com.santiao.publics.util.oaid.DemoHelperHuawei;
import com.santiao.publics.util.oaid.MiitHelper;
import com.santiao.publics.util.oaid.StringId;
import com.santiao.youxiang.ui.adapter.MainViewModel;
import com.santiao.youxiang.utils.CountdownTimeTask;

import java.util.TimerTask;


/**
 * Custom application.
 * <p>
 *
 *
 * @version 1.0
 */
public class ShuidianApplication extends BaseApplication implements Application.ActivityLifecycleCallbacks , MiitHelper.AppIdsUpdater,DemoHelperHuawei.AppIdsUpdater , UmengUtil.AppIdsUpdater {

    private static final String TAG = "QuickFrame";

    public static Context mAppInstance;

    private static com.santiao.youxiang.ShuidianApplication appContext;
    public static MainViewModel main ;

    public static int runningActivities = 0;

    public static boolean isAppInForeground() {
        return runningActivities > 0;
    }
    public   static  boolean ifMainActivity = false;


    private boolean isAppInBackground = true;
    public static ShuidianApplication getInstance() {
        if (appContext == null) {
            appContext = new ShuidianApplication();
        }
        return appContext;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        mAppInstance = getApplicationContext();

        appContext = this;
        main =  ViewModelProvider.AndroidViewModelFactory.getInstance(this).create(MainViewModel.class);

        if (SharedPreferencesUtil.getAgreementStatus()) {
            String odid = SharedPreferencesUtil.getString("oaid");
            //缓存唯一标识
            StringId.idCache(this);
            if(odid ==null || odid.isEmpty()) {

                SplashActivity.mymanufacturer = MyStringUtil.getmanufacturer(Build.MANUFACTURER);

                try {

                    new UmengUtil().getOaid(this,this);

                }catch (Exception e){
                    e.printStackTrace();
                }
                try {

                    if("huawei".equals(SplashActivity.mymanufacturer)) {
                        new DemoHelperHuawei().getOaid(this, this);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

                try {
                    //初始化获取oaid
                    SplashActivity.demoHelper   = new MiitHelper(this);
                    SplashActivity.demoHelper.getDeviceIds(this);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }


        registerActivityLifecycleCallbacks(this);
        setTimeCountdown();
    }

    public static MainViewModel getSharedViewModel(){
        if(main == null){
            main =  ViewModelProvider.AndroidViewModelFactory.getInstance(getInstance()).create(MainViewModel.class);

        }

        return  main;
    }
    public static final Context getMyApplicationContext() {
        return mAppInstance;
    }





    @Override
    public void onActivityPaused(Activity activity) {

        Log.i(Const.TAG,"DaysApplication-------onActivityPaused");
        // 应用程序切换到后台
        isAppInBackground = true;
        // 执行相应的操作
    }

    @Override
    public void onActivityResumed(Activity activity) {
        Log.i(Const.TAG,"DaysApplication-------onActivityResumed");
        // 应用程序回到前台
        if (isAppInBackground) {
            // 执行相应的操作
//            SplashActivity.getConfigAd();
        }
        isAppInBackground = false;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        Log.i(Const.TAG,"DaysApplication-------onActivityCreated");
    }

    @Override
    public void onActivityStarted(Activity activity) {
        Log.i(Const.TAG,"DaysApplication-------onActivityStarted");
        if (runningActivities == 0) {
            Log.i(Const.TAG,"DaysApplication-------onActivityStarted----切换到前台++++ifMainActivity："+ifMainActivity);
            if(ifMainActivity) {

                if (SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD)) {
                    Intent intent = new Intent(this, SplashActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    this.startActivity(intent);
                }
            }

        }

        runningActivities++;
    }



    @Override
    public void onActivityStopped(Activity activity) {
        Log.i(Const.TAG,"DaysApplication-------onActivityStopped");
        runningActivities--;
        if (runningActivities == 0) {
            Log.i(Const.TAG,"DaysApplication-------onActivityStopped---切换到后台");
        }

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        Log.i(Const.TAG,"DaysApplication-------onActivitySaveInstanceState");
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        Log.i(Const.TAG,"DaysApplication-------onActivityDestroyed");
    }
    @Override
    public void OnIdsAvalid(@NonNull String ids) {
        Log.d(Const.TAG, "onIdsValid--11111111111111----oaid:"+ids);
    }
//    @Override
//    public void onIdsValid(String oaid, String vaid, String aaid) {
//
//        Log.d(Const.TAG, "onIdsValid--11111111111111----oaid:"+oaid);
//    }

    @Override
    public void onIdsValid(String oaid) {
        Log.d(Const.TAG, "onIdsValid---huawei--11111111111111----oaid:"+oaid);
    }


    @Override
    public void onIdsValidUm(String oaid) {
        Log.d(Const.TAG, "onIdsValid---ym--11111111111111----oaid:"+oaid);
    }




    private void setTimeCountdown() {
//        CountdownTimeTask countdownTimeTask = new CountdownTimeTask(1000 * 3600, new TimerTask() {
        CountdownTimeTask countdownTimeTask = new CountdownTimeTask(1000 * 15*60, new TimerTask() {
            @Override
            public void run() {
                LogUtil.d(Const.TAG, "CountdownTimeTask调用了----------"+ShuidianApplication.ifMainActivity +"-----"+SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD));
//                if (SharedPreferencesUtil.getStatus(SharedPreferencesUtil.IF_AD)) {
                    if (ShuidianApplication.ifMainActivity) {
                        SplashActivity.updateConfigAd(SplashActivity.activity);


                    }
//                }


            }
        });
        countdownTimeTask.start();
    }

}
